package com.zimmur.tools;

public class CouponCodeUtils {
	private static Object lock=new Object();

	public static String getCode(int total,int used){
		String code="";
		synchronized (lock) {
			if(total>used){
				double i=1;
				int codeNum=0;
				int prefix1=(int)(Math.random()*10);
				int prefix2=(int)(Math.random()*10);
				String code1=null;
				used++;
				if(total<=10000){
					i=10000/total;
					codeNum=(int)(10000-used*i);
					code1=codeNum+"";
					while(code1.length()<4){
						code1=addZero(code1, 4);
					}
					code=prefix1+""+prefix2+code1;
				}else if(total<=1000000){
					i=1000000/total;
					codeNum=(int)(1000000-used*i);
					code1=codeNum+"";
					while(code1.length()<6){
						code1=addZero(code1, 6);
					}
					code=prefix1+""+prefix2+code1;
				}

			}else{
				code="#00";
			}
		}
		
		return code;
	}
	
	private static String addZero(String codeS,Integer num){
		String code=null;
		if(codeS.length()<num){
			code="0"+codeS;
		}
		return code;
	}
	public static void main(String[] args) {
		for(int i=0;i<50;i++){
			String code=getCode(10000, i);
			System.out.println(i+":"+code);
		}
		/*for(int k=0;k<100;k++){
			int prefix1=(int)(Math.random()*10);
			int prefix2=(int)(Math.random()*10);
			String kn=prefix1+""+prefix2;
			System.out.println(kn);
		}*/
	}
}
